package shmallarchitect.web.pages;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlInputSecret;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import shmallarchitect.faces.util.UserUtil;
import shmallarchitect.jpa.entities.User;

public class Register {

    private HtmlInputText login;
    private HtmlInputSecret password;
    private HtmlInputSecret passwordCheck;
    private HtmlInputText firstName;
    private HtmlInputText lastName;
    private HtmlInputText email;
    private final static String AUTH_USER = "Authorized_User";

    public Register() {
    }

    public void validateEmail(FacesContext context, UIComponent toValidate,
            Object value) throws ValidatorException {
        String eMail = (String) value;
        if (eMail.indexOf("@") < 0) {
            FacesMessage message = new FacesMessage("Invalid email address!");
            throw new ValidatorException(message);
        }
    }

    public void validatePassword(FacesContext context, UIComponent toValidate,
            Object value) throws ValidatorException {

        String password1 = (String) this.getPassword().getValue();
        String password2 = (String) value;

        if (!password1.equals(password2)) {
            FacesMessage message = new FacesMessage("Passwords do not match!");
            throw new ValidatorException(message);
        }
    }

    public String registerUser() {

        User newUser = new User();

        newUser.setLogin(login.getValue().toString());
        newUser.setPassword(password.getValue().toString());
        newUser.setFirstName(firstName.getValue().toString());
        newUser.setLastName(lastName.getValue().toString());
        newUser.setEmail(email.getValue().toString());

        if (UserUtil.AddRegisteredUser(newUser) == null) { // userid exists

            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage("Userid " + newUser.getLogin() + " already exists! Please choose another."));
            newUser = null;

            return (null);
        } else {

            // Is a new user, continue logging in
            User managedUserBean = (User) JSFUtil.getManagedObject("UserBean");
            UserUtil.copyUserProperties(newUser, managedUserBean);
            managedUserBean.setIsAdmin(true); //=setIsLoggedIn

            //TODO: disable security filter /does not work yet
            JSFUtil.storeOnSession(FacesContext.getCurrentInstance(), AUTH_USER, "Authorized_User");

            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Registration Successful!"));
            System.out.println("Done Registering User..");
            return ("main");
        }

    }

    public void setFirstName(HtmlInputText firstName) {
        this.firstName = firstName;
    }

    public HtmlInputText getFirstName() {
        return firstName;
    }

    public void setLastName(HtmlInputText lastName) {
        this.lastName = lastName;
    }

    public HtmlInputText getLastName() {
        return lastName;
    }

    public void setEmail(HtmlInputText email) {
        this.email = email;
    }

    public HtmlInputText getEmail() {
        return email;
    }

    public void setLogin(HtmlInputText userid) {
        this.login = userid;
    }

    public HtmlInputText getLogin() {
        return login;
    }

    public void setPassword(HtmlInputSecret password) {
        this.password = password;
    }

    public HtmlInputSecret getPassword() {
        return password;
    }

    public void setPasswordCheck(HtmlInputSecret passwordChk) {
        this.passwordCheck = passwordChk;
    }

    public HtmlInputSecret getPasswordCheck() {
        return passwordCheck;
    }
}
